#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
vector<int> bian[N];
int dep[N];
void predep(int x,int fa){
    dep[x] = dep[fa] + 1;
    for(int v: bian[x]){
        if(v == fa){ 
            continue;
        }
        predep(v,x);
    }
}
int son[N],mdep[N];
bool alive[N];
int tong[N];
int C[N];
int mdep1[N];
int fath[N];
void dfs1(int x,int fa){
    fath[x] = fa;
    mdep[x] = 0;
    mdep1[x] = 0;
    son[x] = 0;
    tong[C[x]] = 0;
    alive[x] = 0;
    for(int v: bian[x]){
        if(v == fa){
            continue;
        }
        dfs1(v,x);
        int swp = mdep[v];
        if(swp > mdep[x]){
            swap(swp,mdep[x]);
        }
        if(swp > mdep1[x]){
            swap(swp,mdep1[x]);
        }
        if(mdep[son[x]] < mdep[v]){
            son[x] = v;
        }
    }
    mdep[x]++;
}
int top[N];
int ans[N];
int cans = 0;
void add(int x){
    if(!x)return;
    if(alive[x]){
        return;
    } 
    alive[x] = 1;
    tong[C[x]]++;
    if(tong[C[x]] == 1){
        cans++;
    }
}
void remove(int x){
    if(!x)return;
    if(!alive[x]){
        return;
    }
    alive[x] = 0;
    tong[C[x]]--;
    if(tong[C[x]] == 0){
        cans--;
    }
}
void dfs(int x,int tf){
    if(!son[x]){
        int cur0 = x;
        int tt = 1;
        while(cur0 != fath[tf]){
            tt--;
            if(!tt){
                add(cur0);
            }
            tt = max(tt,mdep1[cur0] + 1);
            cur0 = fath[cur0];
        }
        tt--;
        if(tt){
            remove(fath[tf]);
        }
        remove(x);
        ans[x] = max(ans[x],cans);
        cur0 = fath[x];
        int cur1 = fath[x];
        while(cur0 != fath[tf]){
            remove(cur1);
            cur1 = fath[cur1];
            remove(cur1);
            cur1 = fath[cur1];
            remove(cur0);
            ans[cur0] = max(cans,ans[cur0]);
            for(int v : bian[cur0]){
                if(v == fath[cur0] || v == son[cur0]){
                    continue;
                }
                add(cur0);
                dfs(v,v);
            }
            remove(cur0);
            cur0 = fath[cur0];
        }
    }else{
        dfs(son[x],tf);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i = 1;i < n;i++){
        int u,v;
        cin>>u>>v;
        bian[u].push_back(v);
        bian[v].push_back(u);
    }
    predep(1,0);
    int mp1 = 1;
    for(int i = 2;i<=n;i++){
        if(dep[i] > dep[mp1]){
            mp1 = i;
        }
    }
    predep(mp1,0);
    int mp2 = 1;
    for(int i = 2;i <= n;i++){
        if(dep[i] > dep[mp2]){
            mp2 = i;
        }
    }
    for(int i = 1;i<=n;i++){
        cin>>C[i];
    }
    cans = 0;
    dfs1(mp1,0);
    dfs(mp1,mp1);
    cans = 0;
    dfs1(mp2,0);
    dfs(mp2,mp2);
    for(int i = 1;i<=n;i++){
        cout<<ans[i]<<'\n';
    }
}
